package com.vitabuybuy.tv.ui.auth.viewmodel

import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.toast.XToast
import com.vitabuybuy.tv.dto.LoginInfoDto
import com.vitabuybuy.tv.dto.TokenAndPhoneDto
import com.vitabuybuy.tv.network.AddressRepository
import com.vitabuybuy.tv.network.AuthRepository
import com.vitabuybuy.tv.network.MineRepository

class AuthViewModel : BaseViewModel() {
    val tokenInfo = MutableLiveData<TokenAndPhoneDto>()
    val imageUrl = MutableLiveData<String>()

    val userInfoDto = MutableLiveData<LoginInfoDto>()
    fun getUserInfo() {
        rxHttpRequest {
            onRequest = {
                userInfoDto.value = MineRepository.getInfoByToken().await()
            }
            onError = {
                defUI.msgEvent.postValue(MessageModel(-100))
                XToast.normal(it.msg)
            }
        }
    }

    fun sendToPhone(fromAction: String?, phone: String, areaCode: String) {
        rxHttpRequest {
            onRequest = {
                if (fromAction == "reset_pwd" || fromAction == "forget_pwd") {
                    AuthRepository.getCodeV2(phone, areaCode).await()
                } else {
                    AuthRepository.getCode(phone, areaCode).await()
                }
                defUI.msgEvent.postValue(MessageModel(1))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun sendToPhone2(phone: String, areaCode: String) {
        rxHttpRequest {
            onRequest = {
                AuthRepository.getCodeV3(phone, areaCode).await()
                defUI.msgEvent.postValue(MessageModel(1))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun validateByPhone(phone: String, code: String, areaCode: String) {
        rxHttpRequest {
            onRequest = {
                AuthRepository.checkPhoneAndCode(code, phone, areaCode).await()
                defUI.msgEvent.postValue(MessageModel(2))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun uploadImage(path: String) {
        rxHttpRequest {
            onRequest = {
                imageUrl.value = AuthRepository.upLoadImage(path).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
            "上传中..."
        }
    }

    fun sendToEmail(email: String) {
        rxHttpRequest {
            onRequest = {
                AuthRepository.getEmailCode(email).await()
                defUI.msgEvent.postValue(MessageModel(4))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun validateByEmail(email: String, code: String) {
        rxHttpRequest {
            onRequest = {
                AuthRepository.checkEmailAndCode(code, email).await()
                defUI.msgEvent.postValue(MessageModel(5))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun register(
        countryCode: String?,
        phone: String?,
        password: String?,
        nickname: String?,
        avatar: String?,
        email: String?,
        gender: String?,
        birthday: String?,
        regionId: Int?
    ) {
        rxHttpRequest {
            onRequest = {
                tokenInfo.value = AuthRepository.register(
                    countryCode,
                    phone,
                    password,
                    nickname,
                    avatar,
                    email,
                    gender,
                    birthday,
                    regionId
                ).await()
            }
            onError = {
                defUI.msgEvent.postValue(MessageModel(-100))
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun loginByPhoneAndPwd(phone: String, password: String, areaCode: String) {
        rxHttpRequest {
            onRequest = {
                tokenInfo.value =
                    AuthRepository.loginByPhoneAndPwd(phone, password, areaCode).await()
            }
            onError = {
                defUI.msgEvent.postValue(MessageModel(-100))
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun loginByPhoneAndCode(phone: String, code: String, areaCode: String) {
        rxHttpRequest {
            onRequest = {
                tokenInfo.value = AuthRepository.loginByPhoneAndCode(phone, code, areaCode).await()
            }
            onError = {
                defUI.msgEvent.postValue(MessageModel(-100))
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun loginByEmailAndPwd(email: String, password: String) {
        rxHttpRequest {
            onRequest = {
                tokenInfo.value = AuthRepository.loginByEmailAndPwd(email, password).await()
            }
            onError = {
                defUI.msgEvent.postValue(MessageModel(-100))
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun resetPasswordByPhone(account: String?, areaCode: String?, password: String) {
        rxHttpRequest {
            onRequest = {
                AuthRepository.resetPasswordByPhone(account, areaCode, password).await()
                defUI.msgEvent.postValue(MessageModel(8))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun resetPasswordByEmail(account: String?, password: String) {
        rxHttpRequest {
            onRequest = {
                AuthRepository.resetPasswordByEmail(account, password).await()
                defUI.msgEvent.postValue(MessageModel(8))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun getRegionList(regionId: Int?) {
        rxHttpRequest {
            onRequest = {
                val it = AddressRepository.getRegionList(regionId).await()
                defUI.msgEvent.postValue(MessageModel(9, obj = it))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun resetUserInfo(key: String, value: Any) {
        rxHttpRequest {
            onRequest = {
                MineRepository.resetUserInfo(key, value).await()
                defUI.msgEvent.postValue(MessageModel(10))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
}
